﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;

using DataAccessEntLib;

public partial class HomePageSearchControl : System.Web.UI.UserControl
{

    BO.Ad bo = new BO.Ad();
    BLL.AdMagnager bll = new BLL.AdMagnager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindLocation();
            BindCategory();
        }
    }

    protected void BindCategory()
    {
        IDataReader dr;
        ddlCategory.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlCategory.Items.Add(lst1);
        dr = bll.SelectAdCategoryMaster();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["AdCategory"].ToString();
                lst.Value = dr["AdCategoryID"].ToString();
                ddlCategory.Items.Add(lst);
                ddlCategory.SelectedIndex = 0;
            }
        }
        dr.Close();
    }

    protected void BindLocation()
    {
        IDataReader dr;
        ddlLocation.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlLocation.Items.Add(lst1);
        //dr = bll.SelectLocationMaster();
        dr = bll.SelectCountryMaster();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                //lst.Text = dr["LandMark"].ToString();
                lst.Text = dr["CountryName"].ToString();
                lst.Value = dr["ID"].ToString();
                ddlLocation.Items.Add(lst);
                ddlLocation.SelectedIndex = 0;
            }
        }
        dr.Close();
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string strSearchValue = string.Empty, ColumnName = string.Empty;

        if (txtName.Text != string.Empty)
        {
            strSearchValue = "AdTitle" + ",";
            Session["Name"] = txtName.Text;
        }
        if (ddlLocation.SelectedItem != null)
        {
            if (ddlLocation.SelectedItem.Text != "Select")
            {
                strSearchValue += "Country" + ",";
                Session["Country"] = ddlLocation.SelectedItem.Text;
            }
            if (ddlCategory.SelectedItem.Text != "Select")
            {
                strSearchValue += "AdCategoryID" + ",";
                Session["CategoryID"] = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
            }
        }
        if (strSearchValue.Length > 0)
        {
            strSearchValue = strSearchValue.Substring(0, strSearchValue.LastIndexOf(","));
            Session["SearchValue"] = strSearchValue;
        }
        Response.Redirect("Search.aspx");
    }
}